package control;

import java.util.ArrayList;

public class WordInfo {
	private String word;
	private ArrayList<Type> wordtypes;
	private int wordnumber;
	
	public WordInfo(String _word, int _wordnumber) {
		this.setWord(_word);
		this.setWordnumber(_wordnumber);
		this.setWordtypes(new ArrayList<Type>());
	}
	
	public WordInfo(String _word) {
		this.setWord(_word);
		this.setWordnumber(0);
		this.setWordtypes(new ArrayList<Type>());
	}
	
	public void AddTypeDef(Type type) {
		if (!type.getType().isEmpty()) {
			if (!wordtypes.contains(type)) {
				wordtypes.add(type);
			}
		}
	}

	public void AddTypeDef(ArrayList<Type> typelist) {
		for (int i = 0; i < typelist.size(); i++) {
			AddTypeDef(typelist.get(i));
		}
		
	}
	
	private void setWord(String word) {
		this.word = word;
	}

	public String getWord() {
		return word;
	}

	private void setWordtypes(ArrayList<Type> wordtypes) {
		this.wordtypes = wordtypes;
	}

	private ArrayList<Type> getWordtypes() {
		return wordtypes;
	}
	
	public String getWordTypeString() {
		String ret = "";
		
		for (int i = 0; i < wordtypes.size(); i++) {
			ret += "'" + wordtypes.get(i).toString() + "', ";
		}
		ret = ret.substring(0, ret.length() - 2);
		
		return ret;
	}
	
	public ArrayList<String> getTypesList() {
		ArrayList<String> ret = new ArrayList<String>();
		for (int i = 0; i < wordtypes.size(); i++) {
			if (!(ret.contains(wordtypes.get(i).getType()))) {
				ret.add(wordtypes.get(i).getType());
			}
		}
		return ret;
	}
	
	public String getSimpleType() {
		for (int i = 0; i < wordtypes.size(); i++) {
			String curtype = wordtypes.get(i).getType();
			if (curtype.equals(Type.PRONOME)
					|| curtype.equals(Type.PREPOSITION)
					|| curtype.equals(Type.ADJECTIVO)
					|| curtype.equals(Type.ADVERBIO)
					|| curtype.equals(Type.ARTIGO)
					|| curtype.equals(Type.NOUN)) {
				return curtype;
			}
		}
		return null;
	}
	
	public boolean hasThisType(String type) {
		
		for (int i = 0; i < wordtypes.size(); i++) {
			if (wordtypes.get(i).getType().equals(type)) {
				return true;
			}
		}
		
		return false;
	}
	
	public WordInfo getFilteredObject(String type) {
		WordInfo ret = new WordInfo(this.word, this.wordnumber);
		
		for (int i = 0; i < wordtypes.size(); i++) {
			if (wordtypes.get(i).getType().equals(type)) {
				ret.AddTypeDef(wordtypes.get(i));
			}
		}
		
		return ret;
	}
	
	public boolean checkTypeFilter(TypeFilter filter) {
		for (int i = 0; i < wordtypes.size(); i++) {
			boolean check = true;
			if (!filter.getType().equals(TypeFilter.NONE)) {
				if (!filter.getType().equals(wordtypes.get(i).getType())) {
					check = false;
				}
			}
			if (!wordtypes.get(i).getGenre().isEmpty() && !filter.getGenre().equals(TypeFilter.NONE)) {
				if (!filter.getGenre().equals(wordtypes.get(i).getGenre())) {
					check = false;
				}
			}
			if (!filter.getPlural().equals(TypeFilter.NONE)) {
				if ((filter.getPlural().equals(TypeFilter.SING)
					&& wordtypes.get(i).getPlural()) ||
					(filter.getPlural().equals(TypeFilter.PLURAL)
							&& !(wordtypes.get(i).getPlural()))) {
					check = false;
				}
			}
			if (!filter.getConjug().equals(TypeFilter.NONE)) {
				if (!filter.getConjug().equals(wordtypes.get(i).getConjug())) {
					check = false;
				}
			}
			if (!filter.getConjug_pers().equals(TypeFilter.NONE)) {
				if (!filter.getConjug_pers().equals(wordtypes.get(i).getConjug_pers())) {
					check = false;
				}
			}
			if (check) {
				return true;
			}
		}
		
		return false;
	}

	public TypeFilter getTypeFilterAccord() {
		TypeFilter ret = new TypeFilter();
		
		for (int i = 0; i < wordtypes.size(); i++) {
			if (ret.getGenre().equals(TypeFilter.NONE)) {
				if (wordtypes.get(i).getGenre().equals(Type.MASCULINO)) {
					ret.setGenre(Type.MASCULINO);
				}
				if (wordtypes.get(i).getGenre().equals(Type.FEMININO)) {
					ret.setGenre(Type.FEMININO);
				}
			} else {
				if (ret.getGenre().equals(Type.MASCULINO)
						&& wordtypes.get(i).getGenre().equals(Type.FEMININO)) {
					ret.setGenre(TypeFilter.NONE);
				}
				if (ret.getGenre().equals(Type.FEMININO)
						&& wordtypes.get(i).getGenre().equals(Type.MASCULINO)) {
					ret.setGenre(TypeFilter.NONE);
				}
			}
			if (ret.getConjug_pers().equals(TypeFilter.NONE)) {
				if (wordtypes.get(i).getConjug_pers().equals(Type.PES_1)) {
					ret.setConjug_pers(Type.PES_1);
				}
				if (wordtypes.get(i).getConjug_pers().equals(Type.PES_2)) {
					ret.setConjug_pers(Type.PES_2);
				}
				if (wordtypes.get(i).getConjug_pers().equals(Type.PES_3)) {
					ret.setConjug_pers(Type.PES_3);
				}
			} else {
				if (ret.getConjug_pers().equals(Type.PES_1)
						&& !(wordtypes.get(i).getConjug_pers().equals(ret.getConjug_pers()))) {
					ret.setConjug_pers(TypeFilter.NONE);
				}
				if (ret.getGenre().equals(Type.PES_2)
						&& !(wordtypes.get(i).getConjug_pers().equals(ret.getConjug_pers()))) {
					ret.setConjug_pers(TypeFilter.NONE);
				}
				if (ret.getGenre().equals(Type.PES_3)
						&& !(wordtypes.get(i).getConjug_pers().equals(ret.getConjug_pers()))) {
					ret.setConjug_pers(TypeFilter.NONE);
				}
			}
			if (ret.getPlural().equals(TypeFilter.NONE)) {
				if (wordtypes.get(i).getPlural()) {
					ret.setPlural(TypeFilter.PLURAL);
				}
				if (!(wordtypes.get(i).getPlural())) {
					ret.setPlural(TypeFilter.SING);
				}
			} else {
				if (ret.getPlural().equals(TypeFilter.PLURAL)
						&& !(wordtypes.get(i).getPlural())) {
					ret.setPlural(TypeFilter.NONE);
				}
				if (ret.getPlural().equals(TypeFilter.SING)
						&& wordtypes.get(i).getPlural()) {
					ret.setPlural(TypeFilter.NONE);
				}
			}
		}
		
		return ret;
	}
	
	public String toString() {
		String ret = this.word + " (";
		
		for (int i = 0; i < wordtypes.size(); i++) {
			ret += "'" + wordtypes.get(i).toString() + "', ";
		}
		ret = ret.substring(0, ret.length() - 2);
		ret += ")";
		
		return ret;
	}

	public void setWordnumber(int wordnumber) {
		this.wordnumber = wordnumber;
	}

	public int getWordnumber() {
		return wordnumber;
	}
}
